package com.swust.course.service.impl;

import com.alibaba.csp.sentinel.util.AssertUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swust.common.core.utils.DateUtils;
import com.swust.course.domain.CourseInfo;
import com.swust.course.mapper.CourseInfoMapper;
import com.swust.course.service.ICourseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程信息Service业务层处理
 *
 * @author LiuShuai
 * @date 2024-04-04
 */
@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements ICourseInfoService {
    @Autowired
    private CourseInfoMapper courseInfoMapper;

    /**
     * 查询课程信息
     *
     * @param courseCode 课程信息主键
     * @return 课程信息
     */
    @Override
    public CourseInfo selectCourseInfoByCourseCode(String courseCode) {
        return courseInfoMapper.selectCourseInfoByCourseCode(courseCode);
    }

    /**
     * 查询课程信息列表
     *
     * @param courseInfo 课程信息
     * @return 课程信息
     */
    @Override
    public List<CourseInfo> selectCourseInfoList(CourseInfo courseInfo) {
        final List<CourseInfo> courseInfos = courseInfoMapper.selectCourseInfoList(courseInfo);
        for (CourseInfo courseInfo1 : courseInfos) {
            if(courseInfo1.getType()==0){
                courseInfo1.setTypeName("理论课");
            }else {
                courseInfo1.setTypeName("实验课");
            }
        }
        return courseInfos;
    }

    /**
     * 新增课程信息
     *
     * @param courseInfo 课程信息
     * @return 结果
     */
    @Override
    public int insertCourseInfo(CourseInfo courseInfo) {
        courseInfo.setCreateTime(DateUtils.getNowDate());
        return courseInfoMapper.insertCourseInfo(courseInfo);
    }

    /**
     * 修改课程信息
     *
     * @param courseInfo 课程信息
     * @return 结果
     */
    @Override
    public int updateCourseInfo(CourseInfo courseInfo) {
        courseInfo.setUpdateTime(DateUtils.getNowDate());
        return courseInfoMapper.updateCourseInfo(courseInfo);
    }

    /**
     * 批量删除课程信息
     *
     * @param courseCodes 需要删除的课程信息主键
     * @return 结果
     */
    @Override
    public int deleteCourseInfoByCourseCodes(String[] courseCodes) {
        return courseInfoMapper.deleteCourseInfoByCourseCodes(courseCodes);
    }

    /**
     * 删除课程信息信息
     *
     * @param courseCode 课程信息主键
     * @return 结果
     */
    @Override
    public int deleteCourseInfoByCourseCode(String courseCode) {
        return courseInfoMapper.deleteCourseInfoByCourseCode(courseCode);
    }

    @Override
    @Transactional
    public String importCourseInfo(List<CourseInfo> courseInfos, boolean updateSupport, String operName) {
        AssertUtil.assertNotEmpty(courseInfos, "导入数据不能为空");

        for (CourseInfo courseInfo : courseInfos) {
            if ("理论课".equals(courseInfo.getCourseName())) {
                courseInfo.setType(0);
            } else {
                courseInfo.setType(1);
            }
        }

        this.saveOrUpdateBatch(courseInfos);
        return "导入成功";
    }
}
